//
//  CLStringHelper.m
//
//  Created by Charlie on 15/5/26.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "NSString+CLExtension.h"
#import <UIKit/UIKit.h>

@implementation NSString (CLPractical)

/// 
+ (NSString *)getUUID:(NSString *)identifier {
    
    UIPasteboard *pb = [UIPasteboard pasteboardWithName:identifier create:YES];
    if ([pb string]){
        return [pb string];
    } else {
        
        CFUUIDRef uuid = CFUUIDCreate(NULL);
        CFStringRef uuidString = CFUUIDCreateString(NULL, uuid);
        NSString *result = [(__bridge NSString *)uuidString copy];
        CFRelease(uuid);
        CFRelease(uuidString);
        
        [pb setString:result];
        return result;
    }
}

///
+ (NSStringEncoding)GBKEncoding {
    NSStringEncoding encoding =
    CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    return encoding;
}

///
+ (NSString*)stringBy:(id)obj {
    
    if ([obj isKindOfClass:NSNumber.class]) {
        return [(NSNumber*)obj stringValue];
        
    } else if ([obj isKindOfClass:NSString.class]) {
        return obj;
        
    }
    //    else if ([obj isKindOfClass:NSNull.class])
    return @"";
    //    else
    //        return [obj description];
}

#pragma mark - 空格处理
/// 移除首尾空格
-(NSString*)removeWhiteSpaceAtFrontAndEnd
{
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

/// 移除多余的空格 例如：@"    this     is a    test    .   "
-(NSString*)removeRedundantWhiteSpace
{
    NSCharacterSet *characterSet = [NSCharacterSet whitespaceCharacterSet];
    NSPredicate *notEmptyPre = [NSPredicate predicateWithFormat:@"SELF != ''"];
    NSArray *parts = [self componentsSeparatedByCharactersInSet:characterSet];
    parts  =[parts filteredArrayUsingPredicate:notEmptyPre];
    return [parts componentsJoinedByString:@" "];
}

/// 移除所有的空格
- (NSString*)removeAllWhiteSpace
{
    return [self stringByReplacingOccurrencesOfString:@" " withString:@""];
}

@end

@implementation NSString (CLValidate)

/// 字符串是否为空
- (BOOL)isEmpty {
    // 判断是否为空
    if ([self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length == 0) return true;
    else return false;
}

/// 判断string的内容是否为网址
- (BOOL)isURL {
    NSString *urlRegex = @"[a-zA-z]+://[^\\s]*";
    NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegex];
    return [urlTest evaluateWithObject:self];
}

/// 邮箱
- (BOOL)isValidateEmail {
    NSString *emailRegex = @"\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:self];
}

/// 手机号码验证
- (BOOL)isValidateMobile {
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString*st = [self stringByReplacingOccurrencesOfString:@"+86" withString:@""];
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0-9])|(17[0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
    return [phoneTest evaluateWithObject:st];
}

/// 是否包含指定字符
- (BOOL)isExistRangeStr:(NSString*)rangeStr {
    return [self rangeOfString:rangeStr].location != NSNotFound;
}

/// 是否包含中文字符
- (BOOL)isExistChinese {
    NSString *chineseRegex = @"[\u4e00-\u9fa5]";
    NSPredicate *chineseTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", chineseRegex];
    for (int i = 0; i < self.length; i++) {
        if ([chineseTest evaluateWithObject:[self substringWithRange:NSMakeRange(i, 1)]]) {
            return YES;
        }
    }
    
    return NO;
}

/// 判断string的内容是否是数字
- (BOOL)isNumeric
{
    NSString *urlRegex = @"[0-9]*";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegex];
    return [predicate evaluateWithObject:self];
}

/// 判断string的内容是否是字母数字
- (BOOL)isAlphanumeric
{
    NSString *urlRegex = @"[a-zA-Z0-9]*";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegex];
    return [predicate evaluateWithObject:self];
}

/// 判断是否为整形
- (BOOL)isInt {
    
    if (self.length < 1) return NO;
    
    NSScanner* scan = [NSScanner scannerWithString:[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
    
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
    
}

/// 判断是否为浮点形：
- (BOOL)isFloat {
    
    NSScanner* scan = [NSScanner scannerWithString:self];
    
    float val;
    
    return [scan scanFloat:&val] && [scan isAtEnd];
    
}

/// 判断是否为Double形：
- (BOOL)isDouble {
    
    NSScanner* scan = [NSScanner scannerWithString:self];
    
    double val;
    
    return [scan scanDouble:&val] && [scan isAtEnd];
}

@end


@implementation NSString (CLPath)

+ (NSString *)documentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

+ (NSString *)cachesDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
}

- (NSString *)inDocumentsDirectoryPath {
    return [[NSString documentsDirectory] stringByAppendingPathComponent:self];
}

- (NSString *)inCachesDirectoryPath {
    return [[NSString cachesDirectory] stringByAppendingPathComponent:self];
}

@end
